import { SingletonFactory } from "./single";

export default class PlayVoice extends SingletonFactory<PlayVoice>() {
  static readonly context = uni.createInnerAudioContext();

  play(src: string, callback?: () => void) {
    return new Promise(resolve => {
      try {
        PlayVoice.context.src = src;
        PlayVoice.context.play();
        PlayVoice.context.onPlay(() => callback?.());
        PlayVoice.context.onEnded(() => {
          resolve(null);
        });
      } catch (error) {
        resolve(null);
      }
    });
  }
  stop() {
    PlayVoice.context.stop();
  }
}
